package com.topvision.auth.service.impl;

import com.topvision.auth.domain.SysUser;
import com.topvision.auth.repository.SysUserRepository;
import com.topvision.auth.service.SysUserService;
import com.topvision.common.exception.BusinessException;
import com.topvision.common.security.SecurityErrorPrompt;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Optional;
/**
 * @author shengwm
 */
@Service
@Slf4j
public class SysUserServiceImpl implements SysUserService {

    @Autowired
    private SysUserRepository sysUserRepository;


    @Override
    public SysUser findById(Long id) {
        return Optional.ofNullable(sysUserRepository.findOne(id)).orElseThrow(() -> new BusinessException(SecurityErrorPrompt.SEED_MICRO_SECURITY_STATUS_10002.getValue(), SecurityErrorPrompt.SEED_MICRO_SECURITY_STATUS_10002.getLabel()));
    }

    @Override
    public Optional<SysUser> findByUserName(String userName) {
        return sysUserRepository.findByUsername(userName);
    }

    @Override
    public void save(SysUser sysUser) {
        sysUserRepository.save(sysUser);
    }
}
